/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package configuration;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author maniksin
 */
public class config_manager {
    
    private boolean ready;
    private Properties prop;
    
    private config_errors error_code;
    //private String error_data;
    
    public boolean is_client;
    public InetAddress server_ip;
    public int server_port;
    public String server_name;
    public String my_name;
    public String log_file;
    public String http_router;
    
    public config_manager(String path) {
        InputStream is;
        ready = true;
        
        // Open the specified config file for reading
        try {
            is = new FileInputStream(path);
        } catch (FileNotFoundException ex) {
            ready = false;
            error_code = config_errors.FILE_NOT_FOUND;
            return;
        }
        
        // Load properties from the file
        prop = new Properties();
        try {
            prop.load(is);
        } catch (IOException ex) {
            ready = false;
            error_code = config_errors.INVALID_FILE;
        }
        
        validate_config();
        if (error_code != config_errors.SUCCESS) {
            ready = false;
        }
    }
    
    public config_errors config_state() {
        if (ready) {
            return config_errors.SUCCESS;
        } else {
            return error_code;
        }
    }
    
    
    private boolean check_prop(String key, config_errors err_num) {
        boolean ret_val = true;
        String prop_val;
        
        if (prop.getProperty(key) == null) {
            error_code = err_num;
            ret_val = false;
        }
        
        return ret_val;
    }
    
    private void validate_config() {
        
        String prop_val;
        
        error_code = config_errors.SUCCESS;
        // Mode should be specified
        prop_val = prop.getProperty("mode");
        if (prop_val.compareToIgnoreCase("client") == 0)  {
            // client mode specified
            // We must have server_ip, server_port
            check_prop("server_ip", config_errors.SERVER_IP_MISSING);
            check_prop("server_port", config_errors.SERVER_PORT_MISSING);
            check_prop("server_name", config_errors.SERVER_NAME_MISSING);
            
            is_client = true;
            
            if (error_code == config_errors.SUCCESS) {
                try {
                    server_ip = InetAddress.getByName(prop.getProperty("server_ip"));
                } catch (UnknownHostException ex) {
                    error_code = config_errors.SERVER_IP_INVALID;
                }
                server_port = Integer.parseInt(prop.getProperty("server_port").toString());
                server_name = prop.getProperty("server_name");
            }
        } else if (prop_val.compareToIgnoreCase("server") == 0) {
            //check_prop("my_ip", config_errors.SELF_IP_MISSING);
            check_prop("my_port", config_errors.SELF_PORT_MISSING);
            is_client = false;
        } else {
            error_code = config_errors.INVALID_MODE;
        }
   
        if (prop.getProperty("my_name") == null) {
            try {
                my_name = InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException ex) {
                error_code = config_errors.MY_NAME_MISSING;
            }
        } else {
            my_name = prop.getProperty("my_name");
        }
        
        http_router = prop.getProperty("http_router");
        
        check_prop("log_file", config_errors.LOG_FILE_MISSING);
        log_file = prop.getProperty("log_file");
        
        // Add more validations here
    }
    
    public int get_my_port(int default_port) {
        if (prop.containsKey("my_port")) {
            return Integer.parseInt(prop.getProperty("my_port").toString());
        } else {
            return default_port;
        }
    }
    
}
